package what.basic.tree;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import what.basic.basiccomponent.*;
import javax.swing.event.*;
public class Jtreeselectionevent implements TreeModelListener,TreeSelectionListener {
	JLabel lbl;
	String nodeName=null;
	DefaultMutableTreeNode root;
	DefaultMutableTreeNode node1;
	DefaultMutableTreeNode node2;
	DefaultMutableTreeNode node3;
	DefaultMutableTreeNode node4;
	JTree tree;
	DefaultTreeModel treemodel;
	Jtreeselectionevent(){
		JFrame f=new Jframe().jframe("defaultmutabletreenode", 300, 00, true);
		Container cp=f.getContentPane();
		 root=new DefaultMutableTreeNode("today to buy");
		 node1=new DefaultMutableTreeNode("vegetable");
		node2=new DefaultMutableTreeNode("fruit");
		node3=new DefaultMutableTreeNode("present");
		node4=new DefaultMutableTreeNode("small househode items");
		root.add(node1);
		root.add(node2);
		root.add(node3);
		root.add(node4);
		DefaultMutableTreeNode leafnode=new DefaultMutableTreeNode("baicai");
		DefaultMutableTreeNode lnode=new DefaultMutableTreeNode("xiaobaicai");
		node1.add(leafnode);
		leafnode.add(lnode);
		tree=new JTree(root);
		tree.setEditable(true);
		tree.addMouseListener(new MouseHandle());
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		final DefaultTreeModel treemodel=(DefaultTreeModel)tree.getModel();
		treemodel.addTreeModelListener(this);
		lbl=new JLabel("change data to:");
		JButton b1=new JButton("add node");
		JButton b2=new JButton ("delete node");
		Container con=new Container();
		con.setLayout(new FlowLayout());
		con.add(b1);
		con.add(b2);
		con.add(lbl);
		JScrollPane sp=new JScrollPane();
		sp.setViewportView(tree);
		cp.add(sp,BorderLayout.CENTER);
		cp.add(con,BorderLayout.SOUTH);
		f.pack();
		b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ev){
				DefaultMutableTreeNode pn=null;
				DefaultMutableTreeNode newnode=new DefaultMutableTreeNode("new node");
				newnode.setAllowsChildren(true);
				TreePath ppath=tree.getSelectionPath();
				pn=(DefaultMutableTreeNode)(ppath.getLastPathComponent());
				treemodel.insertNodeInto(newnode, pn, pn.getChildCount());
				tree.scrollPathToVisible(new TreePath(newnode.getPath()));
				lbl.setText("add new node successed.");
			}
		});
		b2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent eve){
				TreePath treepath=tree.getSelectionPath();
				if(treepath!=null){
					DefaultMutableTreeNode sn=(DefaultMutableTreeNode)treepath.getLastPathComponent();
					TreeNode parent=(TreeNode)sn.getParent();
					if(parent!=null){
						treemodel.removeNodeFromParent(sn);
						lbl.setText("delete node successed!");
					}
				}
			}
		});
		tree.addTreeSelectionListener(new TreeSelectionListener(){

			@Override
			public void valueChanged(TreeSelectionEvent e) {
				DefaultMutableTreeNode node=(DefaultMutableTreeNode)e.getPath().getLastPathComponent();
				lbl.setText("you selected:"+node);
			}
			
		});
	}
	public static void main(String[] args) {
		new Jtreeselectionevent();

	}
	class MouseHandle extends MouseAdapter{
	public void mousePressed(MouseEvent e){
		try{
			JTree tree=(JTree)e.getSource();
			int rowLocation=tree.getRowForLocation(e.getX(), e.getY());
			TreePath treepath=tree.getPathForRow(rowLocation);
			TreeNode treenode=(TreeNode)treepath.getLastPathComponent();
			nodeName=treenode.toString();
		}
		catch(NullPointerException ne){}
	}
	}
	public void treeNodesChanged(TreeModelEvent e) {
		TreePath treepath=e.getTreePath();
		DefaultMutableTreeNode node=(DefaultMutableTreeNode)treepath.getLastPathComponent();
		try{
			int[] index=e.getChildIndices();
			node=(DefaultMutableTreeNode)node.getChildAt(0);
		}
		catch(NullPointerException exc){		}
		lbl.setText(nodeName+" change to :" +(String)node.getUserObject());
	}
	public void treeNodesInserted(TreeModelEvent e) {	}
	public void treeNodesRemoved(TreeModelEvent e) {	}
	public void treeStructureChanged(TreeModelEvent e) {	}
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		// TODO Auto-generated method stub
		
	}

}
